package gamagora.game.entity
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	import gamagora.game.utils.Utils;
	
	import org.flixel.FlxParticle;

	public class OSStall extends OSEntity
	{
		private static const PLANK_RADIUS:Number = 8;
		
		private var _stall:OSBox;
		private var _coinLeft:OSBox;
		private var _coinRight:OSBox;
		private var _circles:Vector.<OSCircle>;
		private var _nbCircles:uint;
		private var _emitter:OSEmitter;
		private var _positions:Vector.<b2Vec2>;
		private var _angles:Vector.<Number>;
		private var _firstAdd:Boolean;
		private var _crash:Boolean;
		
		public function OSStall(X:Number=0, Y:Number=0, h:Number=20, nbRow:uint=4, nbParticles:uint=10)
		{
			super(NOTHING, X, Y);
			_hide = true;
			var w:Number = nbRow * 2 * PLANK_RADIUS + 14;
			_firstAdd = true;
			_crash = false;
			
			// Stall
			_stall = new OSBox(NOTHING, X, Y, w, h, 0, 0);
			_stall.makeGraphic(w, h, 0xff000000);
			_stall.bodyDef.type = b2Body.b2_staticBody;
			_stall.fixDef.friction = 0;
			_stall.fixDef.density = 0;
			_stall.fixDef.restitution = 0;
			
			add(_stall);
		
			// Coins
			_coinLeft = new OSBox(NOTHING, x, y-10, 1, 10);
			_coinLeft.hide = true;
			_coinLeft.makeGraphic(1, 10, 0xff000000);
			_coinRight = new OSBox(NOTHING, x + w - 3, y - 10, 1, 10);
			_coinRight.hide = true;
			_coinRight.makeGraphic(1, 10, 0xff000000);
			
			add(_coinLeft);
			add(_coinRight);
			
			//Sauvegarde des positions et angles
			_positions = new Vector.<b2Vec2>();
			_angles = new Vector.<Number>();
			
			// Circles
			var cpt:uint = nbRow;
			_circles = new Vector.<OSCircle>();
			for(var i:uint = 0; i < nbRow; ++i)
			{
				var xTmp:Number = x + 7 + PLANK_RADIUS * i;
				var yTmp:Number = y - 7 - PLANK_RADIUS - PLANK_RADIUS * 2 * i;
				
				for(var j:uint = 0; j < cpt; ++j)
				{
					var circle:OSCircle = new OSCircle(NOTHING, xTmp, yTmp, PLANK_RADIUS, Utils.getRandomFruit());
					circle.bodyDef.type = b2Body.b2_dynamicBody;
					circle.fixDef.friction = 0.9;
					circle.fixDef.density = 1;			
					add(circle);
					_circles.push(circle);
					
					xTmp = xTmp + PLANK_RADIUS * 2;
				}
				--cpt;
			}
			
			_nbCircles = _circles.length;
			
			// Fragments
			_emitter = new OSEmitter(x + w / 2, y, nbParticles);
			_emitter.setXSpeed(-50, 50);
			_emitter.setYSpeed(-50, 50);
			_emitter.setGravity(50);
			
			var particle:FlxParticle;
			for(i = 0; i < _emitter.getMaxSize(); ++i)
			{			
				particle = new FlxParticle();
				particle.makeGraphic(30, 6, 0xff000000);
				particle.visible = false;
				_emitter.addParticle(particle);
			}
			
			add(_emitter);
		}
		
		private function getRandomFruits() :Class
		{
			var r:uint = uint(Math.random() * 3);
			switch(r)
			{
				case 0:
					return OSImages.ORANGE;
				case 1:
					return OSImages.RED;
				case 2:
					return OSImages.YELLOW;
				default:
					return OSImages.BALL;
			}
		}
		
		//Override
		
		public override function addToWorld():void
		{
			super.addToWorld();
			
			if(_firstAdd)
			{
				for(var i:uint=0;i<_nbCircles;i++)
				{
					_positions.push(new b2Vec2(_circles[i].body.GetPosition().x, _circles[i].body.GetPosition().y));
					_angles.push(_circles[i].body.GetAngle());
				}
				_firstAdd = false;
			}
			else 
			{
				for(i=0;i<_nbCircles;i++)
				{
					_circles[i].body.SetPosition(_positions[i]);
					_circles[i].body.SetAngle(_angles[i]);
				}
			}
		}
		
		public override function removeFromWorld():void
		{
			for(var i:uint=0;i<_nbCircles;i++)
			{
				_positions[i].Set(_circles[i].body.GetPosition().x, _circles[i].body.GetPosition().y);
				_angles[i] = _circles[i].body.GetAngle();
			}
			
			super.removeFromWorld();
		}
		
		public override function update():void
		{
			super.update();
			
			if(!_crash && _world.getPlayerY() > this.y - 20 && _world.getPlayerX() >= this.x - 20 &&  _world.getPlayerX() < this.x + 100)
			{
				_emitter.start(true, 2);
				
				removeEntityFromWorld(_stall);
				remove(_stall);
				
				removeEntityFromWorld(_coinLeft);
				remove(_coinLeft);
				
				removeEntityFromWorld(_coinRight);
				remove(_coinRight);
				
				OSSounds.playWoodCrack1();
				OSSounds.playWoodCrack2();
				OSSounds.playFruits();
				
				_crash = true;
			}
		}
		
	}
}
